package com.bonc.vectorspace.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author donggui@bonc.com.cn
 * @version 2016 2016年6月27日 上午11:39:43
 */
public class EventCorpus {
	
	private List<EventDocument> documents;
	
	private HashMap<String, Set<EventDocument>> invertedIndex;
	
	private Set<String> tags;
	
	public EventCorpus(List<EventDocument> documents) {
		this.documents = documents;
		tags = new HashSet<String>();
		invertedIndex = new HashMap<String, Set<EventDocument>>();
		
		createInvertedIndexAndTag();
	}

	public EventCorpus(List<EventDocument> documents, Boolean noTFIDF) {
		this.documents = documents;
		tags = new HashSet<String>();
		invertedIndex = new HashMap<String, Set<EventDocument>>();
		if(noTFIDF){
			createTag();
		}
		else{	
			createInvertedIndexAndTag();
		}
	}
	
	private void createTag(){
		System.out.println("Creating the tags");
		for (EventDocument document : documents) {
			if(document.getTag() !=null){
				tags.add(document.getTag());
			}
		}
	}
	
	private void createInvertedIndexAndTag() {
		System.out.println("Creating the inverted index");
		
		for (EventDocument document : documents) {
			Set<String> terms = document.getTermList();
			
			//the inverted index
			for (String term : terms) {
				if (invertedIndex.containsKey(term)) {
					Set<EventDocument> list = invertedIndex.get(term);
					list.add(document);
				} else {
					Set<EventDocument> list = new TreeSet<EventDocument>();
					list.add(document);
					invertedIndex.put(term, list);
				}
			}
			
			//the tags
			if(document.getTag() !=null){
				tags.add(document.getTag());
			}
		}
	}
	
	public double getInverseDocumentFrequency(String term) {
		if (invertedIndex.containsKey(term)) {
			double size = documents.size();
			Set<EventDocument> list = invertedIndex.get(term);
			double documentFrequency = list.size();
			
			return Math.log10(size / documentFrequency);
		} else {
			return 0;
		}
	}

	public List<EventDocument> getDocuments() {
		return documents;
	}

	public HashMap<String, Set<EventDocument>> getInvertedIndex() {
		return invertedIndex;
	}

	public Set<String> getTags() {
		return tags;
	}

	public void setTags(Set<String> tags) {
		this.tags = tags;
	}
	
}
